var app = getApp();
Page({

  data: {
    userInfo: null,
    zhicheng:'',
    nickname:'',
    avatar:'',
    avatarUrl:'/assets/images/avatar.png',
  },

  onLoad: function (options) {
 
  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{
      this.getinfo();
    }
  },
  //获取个人信息
  getinfo(){
    var that = this;
    wx.request({
      url: app.apiUrl + '/api/index/getinfo',
      data: {
        openid:wx.getStorageSync('openid'),
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        wx.setStorageSync('userInfo',res.data.data)
        that.setData({ userInfo: res.data.data});

      }
    })
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.saveAvatarInfo(avatarUrl);
    this.setData({
      avatarUrl,
    })
  },

    //保存头像信息
    saveAvatarInfo: function(_path){
      var that = this;
      wx.uploadFile({
        url: app.apiUrl + '/api/index/upAvatar',
        filePath: _path,
        name: "file",
        header: {
          "content-type": "multipart/form-data"
        },
        success: function (res) {
        
          if (res.statusCode == 200) {
            var result = JSON.parse(res.data);
             if(result.code == 1){
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1000
                })
                that.setData({
                  avatar:result.data
                })

             }else{
              wx.showToast({
                title: "上传失败",
                icon: "none",
                duration: 1000
              })
             }
          } 
        },
        fail: function (err) {
          wx.showToast({
            title: "上传失败",
            icon: "none",
            duration: 1000
          })
        },
        complete: function (result) {
          console.log(result.errMsg)
        }
      })

    },

  formSubmit: function (event) {
 
    var that = this;
    if(that.data.avatarUrl == '/assets/images/avatar.png'){
      app.error('请上传头像');
      return;
    }
    console.log(that.data.avatar);

    if (event.detail.value.nickname == '') {
      app.error('微信昵称不能为空');
      return;
    }
    console.log(event.detail.value.nickname);
    if (event.detail.value.phone == '') {
      app.error('手机号不能为空');
      return;
    }
  
    wx.request({
      url: app.apiUrl + '/api/index/zupdate',
      data: {
        phone: event.detail.value.phone,
        openid:wx.getStorageSync('openid'),
        nickname:event.detail.value.nickname,
        avatarUrl:that.data.avatar
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
    
       if(res.data.msg == 2){
        wx.showModal({
          title: '温馨提示',
          content: '查无此号码,请联系管理员',
          showCancel: false
        });
        return false
       }


       if(res.data.msg == 3){
        wx.showModal({
          title: '温馨提示',
          content: '绑定失败,请联系管理员',
          showCancel: false
        });
        return false
       }

       if(res.data.msg == 4){
        wx.showModal({
          title: '温馨提示',
          content: '该手机号已经绑定过账号,请联系管理员',
          showCancel: false
        });
        return false
       }
       
       if(res.data.msg == 5){
        wx.showModal({
          title: '温馨提示',
          content: '您已绑定过账号,若要绑定新账号请联系管理员先解绑',
          showCancel: false
        });
        return false
       }

       if(res.data.msg == 36){
        wx.showModal({
          title: '温馨提示',
          content: '该账号已作废',
          showCancel: false
        });
        return false
       }

       if(res.data.msg == 1){
          console.log(res.data.data)
          wx.showModal({
            title: '温馨提示',
            content: '绑定成功,资料已更新',
            showCancel: true
          });

          this.getinfo();

          //绑定成功 返回首页
          setTimeout(function () { 
            wx.navigateTo({
              url: '/page/my/index',
            })
          }, 2000);
        
       }
     
      }
    })
 
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})